/*
 * This servlet prepares the handed in assignments from a student in a specific course
 */
package controllers.classes;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.AssignmentHandinDao;
import dao.FileDao;
import dao.StudentDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.AssignmentHandin;
import models.Files;
import dao.CourseDao;
import models.Student;
import models.Course;

/**
 *
 * @author Achie
 */
public class HandinViewerController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //get the parameters
        long studentid = Long.parseLong(request.getParameter("studentid"));
        long courseid = Long.parseLong(request.getParameter("courseid"));

        HibernateUtil hu = new HibernateUtil();
        
        //Get all assignments from the course
        FileDao fd = new FileDao(hu.openSession());
        List<Files> allFilesInCourse = fd.findAllFilesInCourse(courseid);
        List<Files> allAssignmentsInCourse = new ArrayList<Files>();
        for(int i=0; i<allFilesInCourse.size(); i++) {
            if(allFilesInCourse.get(i).getType().equals("assignment")) {
                allAssignmentsInCourse.add(allFilesInCourse.get(i));
            }
        }
        request.setAttribute("allassignments", allAssignmentsInCourse); //set all the assignments from a oourse in an attribute
        
        request.setAttribute("courseid", courseid); //important to get back to the same page if grade added
        
        //Get handed in assignment from the course by the student
        AssignmentHandinDao ahd = new AssignmentHandinDao(hu.openSession());
        List<AssignmentHandin> assignmenthandin = ahd.findAllByStudentAndCourse(studentid, courseid); //get the assignments handin in by a studnet in a course
        request.setAttribute("assignmenthandin", assignmenthandin); //set these assignments in an attribute
        
        //Get student object
        StudentDao sd = new StudentDao(hu.openSession());
        Student student = sd.get(studentid); //get the student object
        request.setAttribute("student", student); //set the student object as attribute
        
        CourseDao cd = new CourseDao(hu.openSession());
        Course c = cd.get(courseid); //get the course
        request.setAttribute("course_name", c.getName()); //set the course name as attribute
        
        hu.close(); //close connection
        
        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Student", "Manage student progress"),
            new Breadcrumbs("Student", student.getFirstname()),
            new Breadcrumbs("HandinViewer/view?studentid=" + student.getId() + "&courseid=" + courseid, "Assignments " + student.getFirstname() + " " + student.getInsertion() + " " + student.getLastname()),
        };
        request.setAttribute("breadcrumbs", breadcrumbs);
        
        //send request objects and redirect to the handin assignments (view) page
        RequestDispatcher dispatcher = request.getRequestDispatcher("../jsp/view_handin_assignments.jsp");
        dispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
